package com.ebupt.dbmodule
import android.util.Log
import android.view.View
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.ebupt.common.base.BaseViewModel
import com.ebupt.service.AppUserInfo
import kotlinx.coroutines.flow.Flow

class DBViewModel(private val dbrepo:DBRepo):BaseViewModel() {

    val userList  = MutableLiveData<List<AppUserInfo>>()
    var flowDataSource = Pager(PagingConfig(pageSize = 3)){UserDataSource()}.flow.cachedIn(viewModelScope)
    fun addList(view:View){
        dbrepo.addUser(view.context)
    }

    fun deleteUser(view: View){
       var appUserInfo = userList.value?.get(0)
        Log.e("DB","待删除 : "+appUserInfo.toString())
        appUserInfo?.let {
            dbrepo.deleteUser(view.context,it)
        }
    }

    fun update(view:View){
        var appUserInfo = userList.value?.get(0)
        appUserInfo?.let {
            it.user?.userName = "测试更新"
            dbrepo.updateUser(view.context,it)
        }
    }


    fun query(view: View){
        dbrepo.queryUserNomal(view.context)?.let {
            userList.value = it
            Log.e("DB","大小 : "+(if (it==null) 0 else it?.size))
        }

    }


}